package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.bean.GoodsLimitBean;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

/**
 * Creator: zf.
 * Date: 2020/8/18.
 * Description:限购商品列表-头部横向适配器.
 */
public class GoodsLimitListHeadAdapter extends BaseQuickAdapter<GoodsLimitBean.DataBean, BaseViewHolder> {
    private GoodsLimitListHeadAdapter.OnItemOnClickListener onItemOnClickListener;
    private List<GoodsLimitBean.DataBean> dataBeans = new ArrayList<>();//商品列表
    private RoundedFormation transformation;
    private RoundedFormation transformation1;
    private MultiTransformation<Bitmap> mation;
    private RequestOptions options;
    private int proportion;

    public GoodsLimitListHeadAdapter(@Nullable List<GoodsLimitBean.DataBean> data) {
        super(R.layout.item_goodslimit_head, data);
        this.dataBeans = data;
    }

    @Override
    public void setData(int index, @NonNull GoodsLimitBean.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final GoodsLimitBean.DataBean date) {
        TextView tv_gobuy = viewHolder.getView(R.id.tv_gobuy);
        TextView tv_process = viewHolder.getView(R.id.tv_process);
        TextView tv_oldprice = viewHolder.getView(R.id.tv_oldprice);
        ProgressBar progressBar = viewHolder.getView(R.id.progressBar);
        RelativeLayout rl_gobuy = viewHolder.getView(R.id.rl_gobuy);

        CornerImageView imageView = viewHolder.getView(R.id.img_goods);
        imageView.setType(CornerImageView.TYPE_ROUND);
        viewHolder.setText(R.id.tv_title,date.getGood_title())
                .setText(R.id.tv_oldprice,"¥"+date.getOriginal_price())
                .setText(R.id.tv_newprice,""+date.getConcessional_price())
                .setText(R.id.tv_sale,"今日爆款推荐，已有"+date.getSales_volume()+"人疯抢！");
        tv_oldprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
        ImageGlideUtil.loadImage(mContext,imageView,date.getImage());
        int percent = (int )date.getSales_volume()*100/date.getStock();
        progressBar.setProgress(percent);
        if (date.getSales_volume()>=date.getStock()){
            progressBar.setProgress(100);
            tv_process.setText("已售罄");
            tv_gobuy.setText("已抢光");
            rl_gobuy.setBackgroundResource(R.drawable.db2b2b2_r5);

        }else {
            tv_gobuy.setText("去抢购");
            rl_gobuy.setBackgroundResource(R.drawable.text_color_golimitbuy);
            if (percent>0){
                tv_process.setText(""+percent+"%");
                progressBar.setProgress(percent);
            }else {
                progressBar.setProgress(1);
                tv_process.setText("1%");
            }
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id",date.getGood_id()+"");
                mContext.startActivity(intent);
            }
        });

        tv_gobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (date.getStock()==0){
                    return;
                }
//                Intent intent = new Intent(mContext, XFDGoodsDeatailsActivity.class);
//                intent.putExtra("good_id",date.getGood_id());
//                intent.putExtra("activeType",1);
//                mContext.startActivity(intent);
                onItemOnClickListener.onItemBuyClick(date.getGood_id()+"");
            }
        });
       // Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFineSelect);
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<GoodsLimitBean.DataBean> list){
        this.dataBeans = list;
        super.replaceData(list);
    }

    public void addDatas(List<GoodsLimitBean.DataBean>  list){
        super.addData(list);
        //mData.addAll(list);
    }


    public void setOnItemGoBuyListener(OnItemOnClickListener onItemGoBuy) {
        this.onItemOnClickListener = onItemGoBuy;
    }

    public interface OnItemOnClickListener {
        void onItemBuyClick(String goodsId);
    }

}
